package com;

import java.io.*;

import gnu.io.*;

public class rxtx_write_test {
    private static CommPortIdentifier port;
    private static ParallelPort parallelPort;
    private static Thread readThread;
    private static OutputStream outputStream;

    static String messageString = "三农（和兴机构）销售单\n" +
            "------------------------------\n" +
            "打印时间：2103年07月04日 14：08\n" +
            "单号：\n" +
            "ZCX00001500003201307040100002\n" +
            "收银员：150000301  郭秋霞\n" +
            "会员编号：0\n" +
            "会员姓名：null\n" +
            "    编码    名称\n" +
            "    数量    价格\n" +
            "    优惠    金额 \n" +
            "------------------------------\n" +
            "  115249      分销哈尔滨1号\n" +
            "     1.0    57.0 \n" +
            "     0.0    57.0 \n" +
            "合计：0.0   57.0\n" +
            "收款：      57.0\n" +
            "找零：      0.0\n" +
            "------------------------------\n" +
            "银行卡：	7.0\n" +
            "卡号：6876876\n" +
            "代金券：    7.0\n" +
            "编号：56757676\n" +
            "现金        43.0\n" +
            "------------------------------\n" +
            "地址：小马路12号，电话：12345678\n" +
            "请保留小票，作为退换货的凭证\n";


    static byte[] CashDrawerCmd = new byte[]{0x1b, 0x70, 0x00, (byte) 0x80, (byte) 0x80}; //弹钱箱命令

    public static void main(String[] args) {
        String defaultPort;

        // determine the name of the serial port on several operating systems
        String osname = System.getProperty("os.name", "").toLowerCase();
        System.out.println("操作系统:" + osname);
        if (osname.startsWith("windows")) {
            // windows
            defaultPort = "COM10";
        } else if (osname.startsWith("linux")) {
            // linux
            defaultPort = "/dev/lp0";
        } else if (osname.startsWith("mac")) {
            // mac
            defaultPort = "????";
        } else {
            System.out.println("Sorry, your OS isn't supported.");
            return;
        }

        if (args.length > 0) {
            defaultPort = args[0];
        }

        System.out.println("Default port is: " + defaultPort);

        // parse ports and if the default port is found, initialized the reader
        try {
            port = CommPortIdentifier.getPortIdentifier(defaultPort);
            System.out.println("Port Type = " + port.getPortType());
            System.out.println("Port Name = " + port.getName());
            rxtx_write_test parport_test = new rxtx_write_test();
        } catch (NoSuchPortException e) {
            System.out.println("Target port not found!");
        }
    }

    public rxtx_write_test() {
        // initalize serial port
        try {
            parallelPort = (ParallelPort) port.open("ParportTest", 1000);
        } catch (PortInUseException e) {
        }

      /* Not support by Rxtx
      try {
          parallelPort.setMode(ParallelPort.LPT_MODE_SPP); //this throws errors for some reason
      } catch (UnsupportedCommOperationException e) {}
      */

        try {
            outputStream = parallelPort.getOutputStream();

            System.out.println("Write...");
//         for(int i=0;i<3;i++){//循环打印几次
            outputStream.write(messageString.getBytes("GBK"));//中文编码
//          }
            outputStream.write(CashDrawerCmd);//发送弹钱箱命令

            //outputStream.flush();

            System.out.println("Close...");
            outputStream.close();
        } catch (IOException e) {
        }

        System.exit(0);
    }
}
